package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.Camera;
import com.grad.why.studentsystem.domain.Project;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.domain.current.CurrentProject;
import com.grad.why.studentsystem.mapper.ProjectMapper;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.ProjectService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Service(interfaceClass = ProjectService.class)
@Transactional

public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Project> getProjects() {
        try{
            return projectMapper.getProjects();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Project getProjectByPrimaryKey(Integer projectid) {
        try{
            return projectMapper.selectByPrimaryKey(projectid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CurrentProject> getProjectList(int offset, int limit, String sort, String search) {
        try{
            return projectMapper.getProjectList(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getProjectListLength(String search) {
        try{
            return projectMapper.getProjectListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer getMaxId() {
        return projectMapper.getMaxId();
    }

    @Override
    public int ProjectNameIsExist(String name) {
        return projectMapper.ProjectNameIsExist(name);
    }

    @Override
    public int addProjectByEntity(Project project) {
        return projectMapper.insertSelective(project);
    }


    @Override
    public int addProjectInfosByExcelFile(String uploadCurrentFileName) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            DecimalFormat decimalFormat = new DecimalFormat("##0");//格式化设置
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }

            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;
            Row title = wb.getSheetAt(0).getRow(0);
            if(!title.getCell(0).getStringCellValue().equals("专业名称")){
                return 0;
            }

            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                Project project = new Project();
                                project.setId(projectMapper.getMaxId() + 1);
                                project.setName(Util.formGetCellType(row.getCell(0)));
                                project.setStudentcount(0);
                                projectMapper.insertSelective(project);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteProjectinfo(int id) {
        try{
            int n = projectMapper.deleteByPrimaryKey(id);
            return n;
        }catch (Exception E){
            E.printStackTrace();
        }
        return 0;
    }
}
